package com.cmy.b_claculator.ui.add

import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.widget.ArrayAdapter
import android.widget.ListPopupWindow
import android.widget.Toast
import com.bigkoo.pickerview.TimePickerView
import com.blankj.utilcode.util.TimeUtils
import com.cmy.b_claculator.R
import com.cmy.b_claculator.databinding.FragmentAddWorkBinding
import com.cmy.b_claculator.db.ClaculatorDatabase
import com.cmy.b_claculator.db.dao.WorkDateDao
import com.cmy.b_claculator.db.entity.*
import com.example.baselibrary.app.base.BaseFragment
import com.example.baselibrary.app.ext.initClose
import kotlinx.android.synthetic.main.include_toolbar.*
import me.hgj.jetpackmvvm.ext.nav
import java.util.*

class AddWorkFragment : BaseFragment<AddWorkViewModel, FragmentAddWorkBinding>() {
    override fun layoutId(): Int= R.layout.fragment_add_work
   private var listspeciesPopupWindow:ListPopupWindow? = null
    private var listUnitPopupWindow:ListPopupWindow? = null

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewModel = mViewModel

        arguments?.let {
            mViewModel.id = it.getLong("ID")
        }
        toolbar.run {
            initClose("添加") {
                nav().navigateUp()
            }
        }

        mDatabind.tvTime.setOnClickListener {
            showTimeChoose()
        }

        mDatabind.submit.setOnClickListener {
           mViewModel. submit()
        }


        mViewModel.initData()
    }



    override fun createObserver() {
        super.createObserver()
        mViewModel.speciesStr.observe(this, {
                val list = mViewModel.querySpecies(it)
                if ((list.isNullOrEmpty() &&  listspeciesPopupWindow != null && listspeciesPopupWindow!!.isShowing) || TextUtils.isEmpty(it) ){
                    listspeciesPopupWindow?.dismiss()
                }else{
                    if (mViewModel.selecteSpeciesEntity == null || it != mViewModel.selecteSpeciesEntity?.name)
                        showSpeciesListPopulWindow(list)

                }

        })
        mViewModel.unitStr.observe(this,{
                val list =  mViewModel.queryUnit(it)
                if ((list.isNullOrEmpty() && listUnitPopupWindow != null && listUnitPopupWindow!!.isShowing )  || TextUtils.isEmpty(it)) {
                    listUnitPopupWindow?.dismiss()
                } else {
                    if (mViewModel.selecteUnitEntity == null || it != mViewModel.selecteUnitEntity?.name)
                        showUnitListPopulWindow(list)
                }

        })
    }


    /**选择种类*/
    private fun showSpeciesListPopulWindow(list: MutableList<String>) {
        if (listspeciesPopupWindow == null){
            listspeciesPopupWindow = activity?.let { ListPopupWindow(it) }
        }

        listspeciesPopupWindow?.let {
                it.anchorView = mDatabind.etSpecies
                it.setOnItemClickListener { _, _, i, _ ->
                    mViewModel.selectSpeciesListPop(list[i])
                    it.dismiss()
                }

                    it.setAdapter(activity?.let { it1 ->
                        ArrayAdapter(it1,
                            android.R.layout.simple_list_item_1,
                            list)
                    })
                    it.show()
            }
    }

    /**选择单位*/
    private fun showUnitListPopulWindow(list: MutableList<String>) {
        if (listUnitPopupWindow == null) {
            listUnitPopupWindow = activity?.let { ListPopupWindow(it) }
        }
        listUnitPopupWindow?.let {
            it.anchorView = mDatabind.etSpecies
            it.setOnItemClickListener { _, _, i, _ ->
                mViewModel.selectUnitListPop(list[i])
                it.dismiss()
            }

            it.setAdapter(activity?.let { it1 ->
                ArrayAdapter(it1,
                    android.R.layout.simple_list_item_1,
                    list)
            })
            it.show()
        }
    }


    /**
     * 时间选择器
     */
    private fun showTimeChoose(){
//控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        val selectedDate = Calendar.getInstance()
        val startDate = Calendar.getInstance()
        startDate.set(2013, 0, 23)
        val endDate = Calendar.getInstance()
        endDate.set(2029, 12, 30)

        val type =  booleanArrayOf(true, true, true, false, false, false)
        //时间选择器
        val pvTime =  TimePickerView.Builder(activity
        ) { date, _ ->
            mViewModel.timeStr.set( TimeUtils.date2String(date,"yyyy-MM-dd"))
        }
            //年月日时分秒 的显示与否，不设置则默认全部显示
            .setType(type)
            .setLabel("年", "月", "日", "时", "分", "秒")
            .isCenterLabel(true)
            .setDividerColor(Color.DKGRAY)
            .setContentSize(16)//字号
            .setDate(selectedDate)
            .setRangDate(startDate, endDate)
            .setDecorView(null)
            .build()

        pvTime.show()

    }



}